home *** CD-ROM | disk | FTP | other *** search
/ Tech Arsenal 1 / Tech Arsenal (Arsenal Computer).ISO / tek-01 / pckey.zip / PCKEY.HPP < prev   
C/C++ Source or Header  |  1991-03-24  |  7KB  |  306 lines

  1. /*
  2.  
  3.     pckey.hpp
  4.     3-24-91
  5.     PC keyboard class for Borland C++
  6.  
  7.     Copyright 1991
  8.     John W. Small
  9.     All rights reserved
  10.     Use freely but acknowledge authorship and copyright.
  11.     CIS: 73757,2233
  12.  
  13.     PSW / Power SoftWare
  14.     P.O. Box 10072
  15.     McLean, Virginia 22102 8072
  16.     (703) 759-3838
  17.  
  18. */
  19.  
  20. #ifndef PCKEY_CPP
  21. #define PCKEY_CPP
  22.  
  23. #include <dos.h>
  24.  
  25.  
  26. //  Typematic Rate = 10 x characters per second
  27.  
  28. enum TypeMaticRate  { TMR300, TMR267, TMR240, TMR218,
  29.     TMR200, TMR185, TMR171, TMR160, TMR150, TMR133,
  30.     TMR120, TMR109, TMR100, TMR092, TMR086, TMR080,
  31.     TMR075, TMR067, TMR060, TMR055, TMR050, TMR046,
  32.     TMR043, TMR040, TMR037, TMR033, TMR030, TMR027,
  33.     TMR025, TMR023, TMR021, TMR020 };
  34.  
  35.  
  36. //  Typematic Delay in milliseconds
  37.  
  38. enum TypeMaticDelay  { TMD250, TMD500, TMD750, TMD1000 };
  39.  
  40.  
  41.  
  42. //  Do not instantiate!
  43.  
  44. class PCKey  {
  45. // Variables are static to save having to dereference
  46. // the "this" pointer in the inline member functions.
  47. static    unsigned asciiScan;
  48. static    unsigned char enhancedKeyBrd;
  49. static    unsigned enhancedShiftMask;
  50. public:
  51.     PCKey();
  52.     int enhanced()  { return (int) enhancedKeyBrd; }
  53.     int getch()  {
  54.         _AH = enhancedKeyBrd;
  55.         geninterrupt(0x16);
  56.         asciiScan = _AX;
  57.         return (int) _AL;
  58.     }
  59. // Ascii code of last character read with getch()
  60. // or character waiting with kbhit().
  61.     int ascii()
  62.         { return (int)(asciiScan & 0x00FF); }
  63. // Scan code of last character read with gethch()
  64. // or character waiting with kbhit().
  65.     int scan()
  66.         { return (int)((asciiScan &0xFF00) >> 8); }
  67.     int kbhit()  {
  68.         _AH = 0x01 | enhancedKeyBrd;
  69.         geninterrupt(0x16);
  70.         asciiScan = _AX;
  71.         return !(int)(_FLAGS & 0x0040);
  72.     }
  73.     unsigned shift()  {
  74.         _AH = 0x02 | enhancedKeyBrd;
  75.         geninterrupt(0x16);
  76.         return (_AX & enhancedShiftMask);
  77.     }
  78.     unsigned InsertStateActive()
  79.         { return (shift() & 0x0080); }
  80.     unsigned CapsLockActive()
  81.         { return (shift() & 0x0040); }
  82.     unsigned NumLockActive()
  83.         { return (shift() & 0x0020); }
  84.     unsigned ScrollLockActive()
  85.         { return (shift() & 0x0010); }
  86.     unsigned AltPressed()
  87.         { return (shift() & 0x0008); }
  88.     unsigned CtrlPressed()
  89.         { return (shift() & 0x0004); }
  90.     unsigned LeftShiftPressed()
  91.         { return (shift() & 0x0002); }
  92.     unsigned RightShiftPressed()
  93.         { return (shift() & 0x0001); }
  94.     unsigned ShiftPressed()
  95.         { return (shift() & 0x0003); }
  96.     unsigned SysReqPressed()
  97.         { return (shift() & 0x8000); }
  98.     unsigned CapsLockPressed()
  99.         { return (shift() & 0x4000); }
  100.     unsigned NumLockPressed()
  101.         { return (shift() & 0x2000); }
  102.     unsigned ScrollLockPressed()
  103.         { return (shift() & 0x1000); }
  104.     unsigned RightAltPressed()
  105.         { return (shift() & 0x0800); }
  106.     unsigned RightCtrlPressed()
  107.         { return (shift() & 0x0400); }
  108.     unsigned LeftAltPressed()
  109.         { return (shift() & 0x0200); }
  110.     unsigned LeftCtrlPressed()
  111.         { return (shift() & 0x0100); }
  112.     void setTypeMatic(enum TypeMaticRate TMrate,
  113.         enum TypeMaticDelay TMdelay)  {
  114.         _BX = (TMdelay << 8) | TMrate;
  115.         _AX = 0x0305;
  116.         geninterrupt(0x16);
  117.     }
  118.     int putch(unsigned asciiScan)  {
  119.         _CX = asciiScan;
  120.         _AH = 0x05;
  121.         geninterrupt(0x16);
  122.         return !((int) _AL);
  123.     }
  124.     void flush();
  125.     ~PCKey() {}
  126. };
  127.  
  128. extern PCKey PC;  // Only instant, do not instantiate!
  129.  
  130.  
  131. //  Note: Ctrl keys are not special!
  132. //  Returned by getch() and ascii().
  133.  
  134. #define  CtrlA     1
  135. #define  CtrlB     2
  136. #define  CtrlC     3
  137. #define  CtrlD     4
  138. #define  CtrlE     5
  139. #define  CtrlF     6
  140. #define  CtrlG     7
  141. #define  CtrlH     8
  142. #define  CtrlI     9
  143. #define  CtrlJ    10
  144. #define  CtrlK    11
  145. #define  CtrlL    12
  146. #define  CtrlM    13
  147. #define  CtrlN    14
  148. #define  CtrlO    15
  149. #define  CtrlP    16
  150. #define  CtrlQ    17
  151. #define  CtrlR    18
  152. #define  CtrlS    19
  153. #define  CtrlT    20
  154. #define  CtrlU    21
  155. #define  CtrlV    22
  156. #define  CtrlW    23
  157. #define  CtrlX    24
  158. #define  CtrlY    25
  159. #define  CtrlZ    26
  160.  
  161.  
  162. //  Special Key Codes returned via scan() when getch() == 0.
  163.  
  164. #define  AltA         30
  165. #define  AltB         48
  166. #define  AltC         46
  167. #define  AltD         32
  168. #define  AltE         18
  169. #define  AltF         33
  170. #define  AltG         34
  171. #define  AltH         35
  172. #define  AltI         23
  173. #define  AltJ         36
  174. #define  AltK         37
  175. #define  AltL         38
  176. #define  AltM         50
  177. #define  AltN         49
  178. #define  AltO         24
  179. #define  AltP         25
  180. #define  AltQ         16
  181. #define  AltR         19
  182. #define  AltS         31
  183. #define  AltT         20
  184. #define  AltU         22
  185. #define  AltV         47
  186. #define  AltW         17
  187. #define  AltX         45
  188. #define  AltY         21
  189. #define  AltZ         44
  190.  
  191. #define  Home         71
  192. #define  UpArr        72
  193. #define  PgUp         73
  194. #define  LArr         75
  195. #define  RArr         77
  196. #define  EndKey       79
  197. #define  DnArr        80
  198. #define  PgDn         81
  199. #define  InsKey       82
  200. #define  DelKey       83
  201.  
  202. #define  CtrlHome    119
  203. #define  CtrlPgUp    132
  204. #define  CtrlLArr    115
  205. #define  CtrlRArr    116
  206. #define  CtrlEnd     117
  207. #define  CtrlPgDn    118
  208.  
  209. #define  Alt1        120
  210. #define  Alt2        121
  211. #define  Alt3        122
  212. #define  Alt4        123
  213. #define  Alt5        124
  214. #define  Alt6        125
  215. #define  Alt7        126
  216. #define  Alt8        127
  217. #define  Alt9        128
  218. #define  Alt0        129
  219.  
  220. #define  AltHyphen   130
  221. #define  AltEquals   131
  222. #define  CtrlPrtSc   114
  223. #define  ShiftTab     15
  224.  
  225.  
  226. #define  F1           59
  227. #define  ShiftF1      84
  228. #define  CtrlF1       94
  229. #define  AltF1       104
  230.  
  231. #define  F2           60
  232. #define  ShiftF2      85
  233. #define  CtrlF2       95
  234. #define  AltF2       105
  235.  
  236. #define  F3           61
  237. #define  ShiftF3      86
  238. #define  CtrlF3       96
  239. #define  AltF3       106
  240.  
  241. #define  F4           62
  242. #define  ShiftF4      87
  243. #define  CtrlF4       97
  244. #define  AltF4       107
  245.  
  246. #define  F5           63
  247. #define  ShiftF5      88
  248. #define  CtrlF5       98
  249. #define  AltF5       108
  250.  
  251. #define  F6           64
  252. #define  ShiftF6      89
  253. #define  CtrlF6       99
  254. #define  AltF6       109
  255.  
  256. #define  F7           65
  257. #define  ShiftF7      90
  258. #define  CtrlF7      100
  259. #define  AltF7       110
  260.  
  261. #define  F8           66
  262. #define  ShiftF8      91
  263. #define  CtrlF8      101
  264. #define  AltF8       111
  265.  
  266. #define  F9           67
  267. #define  ShiftF9      92
  268. #define  CtrlF9      102
  269. #define  AltF9       112
  270.  
  271. #define  F10          68
  272. #define  ShiftF10     93
  273. #define  CtrlF10     103
  274. #define  AltF10      113
  275.  
  276. //  Some BIOS' don't return these.
  277.  
  278. #define  F11         133
  279. #define  ShiftF11    135
  280. #define  CtrlF11     137
  281. #define  AltF11      139
  282.  
  283. #define  F12         134
  284. #define  ShiftF12    136
  285. #define  CtrlF12     138
  286. #define  AltF12      140
  287.  
  288.  
  289. //  Non special - standard character codes.
  290.  
  291. #define  ESC          27
  292. #define  CR           13
  293. #define  Tab           9
  294. #define  BackSp        8
  295. #define  Space        32
  296. #define  DelCh       127
  297.  
  298. //  Returned by extended keys on 101/102 keyboards
  299. //  that duplicate keys available on 84 key format.
  300. //  Consider the home key ...
  301. //  On number pad:  PC.ascii() ==   0  PC.scan() == 71
  302. //  By itself:      PC.ascii() == 224  PC.scan() == 71
  303. #define  EntendKey   224
  304.  
  305. #endif
  306.